<?php

/*
 * Built by :
 * Andi Kurniawan (fandikurniawan89@gmail.com) at system analis
 */

class Dosen extends CI_Controller {

    function __construct() {
        parent::__construct();
        $login = $this->session->userdata('login');
        $privilege = $this->session->userdata('pri');
        if ($login != TRUE || $privilege != 2) {
            redirect('main/logout');
        }
    }

    function index() {
        redirect('dosen/peserta');
    }

    function home() {
        $nim = $this->session->userdata('username');
        $nm = $this->mmr->get_nama_dosen($nim);
        $na['nama'] = $nm->nama;
        $na['nip'] = $nm->nip;
        $na['photo'] = $nm->pic;
        $this->parser->parse('dosen/header', $na);
        $this->load->view('dosen/nav-bar');
        $this->load->view('dosen/sidebar');
        $this->load->view('dosen/notif');
        $this->load->view('dosen/footer');
    }

    //start bimbingan proposal
    function peserta() {
        $id = $this->session->userdata('username');
        $nim = $this->session->userdata('username');
        $nm = $this->mmr->get_nama_dosen($nim);
        $na['nama'] = $nm->nama;
        $na['nip'] = $nm->nip;
        $na['photo'] = $nm->pic;
        if ($this->mmr->peserta_bimbingan($id)) {
            $dat['tesis'] = $this->mmr->peserta_bimbingan($id);
        } else {
            $dat['tesis'] = array();
        }
        $this->parser->parse('dosen/header', $na);
        $this->load->view('dosen/nav-bar');
        $this->load->view('dosen/sidebar');
        $this->parser->parse('dosen/peserta', $dat);
        $this->load->view('dosen/footer');
    }

    function proposal() {
        $id = $this->uri->segment(3);
        $user = $this->session->userdata('username');
        $nim = $this->session->userdata('username');
        $nm = $this->mmr->get_nama_dosen($nim);
        $na['nama'] = $nm->nama;
        $na['nip'] = $nm->nip;
        $na['photo'] = $nm->pic;
        if ($this->mmr->bimbingan_proposal_dosen($id, $user)) {
            $dat['tesis'] = $this->mmr->bimbingan_proposal_dosen($id, $user);
        } else {
            $dat['tesis'] = array();
        }
        $mhs = $this->mmr->get_mhs($id);
        $dat['nama'] = $mhs->nama;
        $dat['telp'] = $mhs->telp;
        $dat['email'] = $mhs->email;
        $this->parser->parse('dosen/header', $na);
        $this->load->view('dosen/nav-bar');
        $this->load->view('dosen/sidebar');
        $this->parser->parse('dosen/bimbingan-proposal', $dat);
        $this->load->view('dosen/footer');
    }

    function saranProposal() {
        $nim = $this->session->userdata('username');
        $nm = $this->mmr->get_nama_dosen($nim);
        $na['nama'] = $nm->nama;
        $na['nip'] = $nm->nip;
        $na['photo'] = $nm->pic;
        $kode = $this->uri->segment(3);
        $data['id'] = $kode;
        $koe = $this->uri->segment(4);
        $data['hash'] = $koe;
        $da = $this->mmr->get_babSaran($kode);
        $data['bab'] = $da->bab;
        $this->parser->parse('dosen/header', $na);
        $this->load->view('dosen/nav-bar');
        $this->load->view('dosen/sidebar');
        $this->parser->parse('dosen/view-saran', $data);
        $this->load->view('dosen/footer');
    }

    function save_saran() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'doc|docx|rar';
        $config['max_size'] = '10000';
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload()) {
            $nim = $this->session->userdata('username');
            $nm = $this->mmr->get_nama_dosen($nim);
            $na['nama'] = $nm->nama;
            $na['nip'] = $nm->nip;
            $na['photo'] = $nm->pic;
            $data['kode'] = $this->input->post('kode');
            $this->parser->parse('dosen/header', $na);
            $this->load->view('dosen/nav-bar');
            $this->load->view('dosen/sidebar');
            $this->parser->parse('dosen/view-saran', $data);
            $this->load->view('dosen/footer');
        } else {
            $file = $this->upload->data();
            $kode = $this->input->post('id');
            $hash = $this->input->post('hash');
            $da = $this->mmr->get_babSaran($kode);
            $kd = $da->kode;
            $input = array(
                'hash' => $this->encrypt->sha1($file['file_name']) . $this->input->ip_address(),
                'bab' => $this->input->post('bab'),
                'kode' => $kd,
                'file_name' => $file['file_name'],
                'full_path' => base_url() . 'uploads/' . $file['file_name'],
                'file_ext' => $file['file_ext'],
                'file_size' => $file['file_size'],
                'time' => gmdate('Y-m-d'),
                'status' => 'Revision',
                'uploader' => $this->session->userdata('username')
            );
            $this->mmr->insert_upload($input);
            $insert = array(
                'id' => $hash,
                'saran' => $this->input->post('saran')
            );
            $this->mmr->proposal_saran_save($insert);
            $data = array('status' => 'Revision', 'koreksi' => gmdate('Y-m-d'));
            $this->mmr->update_status($hash, $data);
            /* em */
//            $ma = $this->mmr->get_mail_mhs($kd);
//            $email = $ma->email;
//            $this->load->library('email');
//            $this->email->clear();
//            $this->email->to($email);
//            $this->email->from('noreply@andikurniawan.com');
//            $this->email->subject('Bimbingan Proposal.');
//            $this->email->message('File anda tela direvisi, silahkan lihat saran di web. Terima kasih.');
//            $this->email->send();
            /* end em */
            $alert['alert'] = '<div class="alert alert-info"><strong>File berhasil diupload!</strong></div>';
            $data['kode'] = $this->input->post('kode');
            $nim = $this->session->userdata('username');
            $nm = $this->mmr->get_nama_dosen($nim);
            $na['nama'] = $nm->nama;
            $na['nip'] = $nm->nip;
            $na['photo'] = $nm->pic;
            $this->parser->parse('dosen/header', $na);
            $this->load->view('dosen/nav-bar');
            $this->load->view('dosen/sidebar');
            $this->parser->parse('dosen/alert', $alert);
            $this->load->view('dosen/footer');
        }
    }

    function confirm() {
        $id = $this->uri->segment(4);
        $data = array('status' => 'Approved', 'koreksi' => gmdate('Y-m-d'));
        $this->mmr->confirm_proposal_dosen($id, $data);
        $kode = $id;
        $get_bab = $this->mmr->get_bab($kode);
        $bab = $get_bab->bab;
        $nim = $get_bab->uploader;
        $m = $this->mmr->email_mhs($nim);
        $email = $m->email;
        $this->load->library('email');
        $this->email->clear();
        $this->email->to($email);
        $this->email->from('noreply@andikurniawan.com');
        $this->email->subject('Bimbingan Proposal.');
        $this->email->message('Selamat, bimbingan anda telah disetujui, silahkan melanjutkan ke tahap berikutnya. Untuk informasi lebih detail silahkan lihat di web. Terima kasih.');
        $this->email->send();
        if ($bab == 'PROPOSAL BAB III') {
            $id = $this->session->userdata('username');
            $data = array(
                'id' => $this->encrypt->sha1($id . $this->input->ip_address() . gmdate('Y-m-d')),
                'nim' => $nim,
                'ip' => $this->input->ip_address()
            );
            $this->mmr->regis_proposal($data);
            $this->email->clear();
            $this->email->to($email);
            $this->email->from('noreply@andikurniawan.com');
            $this->email->subject('Bimbingan Proposal.');
            $this->email->message('Selamat, bimbingan anda telah disetujui. Anda telah terdafta ke seminar proposal secara otomatis. Untuk melihat jadwal seminar proposal, silahkan lihat di web minimal 1x24 jam setelah email ini dikirim. Terima kasih.');
            $this->email->send();
        }
        redirect('dosen/peserta');
    }

    function download() {
        $this->load->helper('download');
        $id = $this->uri->segment(3);
        $path = $this->mmr->get_link_dosen($id);
        $data = file_get_contents($path->full_path); // Read the file's contents
        $name = $path->file_name;
        force_download($name, $data);
        redirect('dosen/proposal');
    }

    //end bimbingan proposal
    //start tesis
    function tesis() {
        $id = $this->session->userdata('username');
        if ($this->mmr->peserta_tesis($id)) {
            $dat['tesis'] = $this->mmr->peserta_tesis($id);
        } else {
            $dat['tesis'] = array();
        }
        $nim = $this->session->userdata('username');
        $nm = $this->mmr->get_nama_dosen($nim);
        $na['nama'] = $nm->nama;
        $na['nip'] = $nm->nip;
        $na['photo'] = $nm->pic;
        $this->parser->parse('dosen/header', $na);
        $this->load->view('dosen/nav-bar');
        $this->load->view('dosen/sidebar');
        $this->parser->parse('dosen/tesis', $dat);
        $this->load->view('dosen/footer');
    }

    function detail() {
        $id = $this->uri->segment(3);
        $user = $this->session->userdata('username');
        if ($this->mmr->bimbingan_tesis_dosen($id, $user)) {
            $dat['tesis'] = $this->mmr->bimbingan_tesis_dosen($id, $user);
        } else {
            $dat['tesis'] = array();
        }
        $mhs = $this->mmr->get_mhs($id);
        $dat['nama'] = $mhs->nama;
        $dat['telp'] = $mhs->telp;
        $dat['email'] = $mhs->email;
        $nim = $this->session->userdata('username');
        $nm = $this->mmr->get_nama_dosen($nim);
        $na['nama'] = $nm->nama;
        $na['nip'] = $nm->nip;
        $na['photo'] = $nm->pic;
        $this->parser->parse('dosen/header', $na);
        $this->load->view('dosen/nav-bar');
        $this->load->view('dosen/sidebar');
        $this->parser->parse('dosen/bimbingan-tesis', $dat);
        $this->load->view('dosen/footer');
    }

    function confirmTesis() {
        $id = $this->uri->segment(4);
        $data = array('status' => 'Approved', 'koreksi' => gmdate('Y-m-d'));
        $this->mmr->confirm_proposal_dosen($id, $data);
        $kode = $id;
        $get_bab = $this->mmr->get_bab($kode);
        $bab = $get_bab->bab;
        $nim = $get_bab->uploader;
        $m = $this->mmr->email_mhs($nim);
        $email = $m->email;
        $this->load->library('email');
        $this->email->clear();
        $this->email->to($email);
        $this->email->from('noreply@andikurniawan.com');
        $this->email->subject('Bimbingan Proposal.');
        $this->email->message('Selamat, bimbingan anda telah disetujui, silahkan melanjutkan ke tahap berikutnya. Untuk informasi lebih detail silahkan lihat di web. Terima kasih.');
        $this->email->send();
        if ($bab == 'BAB III') {
            $id = $this->session->userdata('username');
            $data = array(
                'id' => $this->encrypt->sha1($id . $this->input->ip_address() . gmdate('Y-m-d H"m:i')),
                'nim' => $nim,
                'ip' => $this->input->ip_address(),
                'type' => 'Tesis'
            );
            $this->mmr->regis_proposal($data);
            $this->email->clear();
            $this->email->to($email);
            $this->email->from('noreply@andikurniawan.com');
            $this->email->subject('Bimbingan Proposal.');
            $this->email->message('Selamat, bimbingan anda telah disetujui. Anda telah terdafta ke seminar secara otomatis. Untuk melihat jadwal seminar, silahkan lihat di web minimal 1x24 jam setelah email ini dikirim. Terima kasih.');
            $this->email->send();
            print_r($data);
        }
        redirect('dosen/tesis');
    }

    function saranTesis() {
        $kode = $this->uri->segment(3);
        $data['id'] = $kode;
        $da = $this->mmr->get_bab($kode);
        $data['bab'] = $da->bab;
        $nim = $this->session->userdata('username');
        $nm = $this->mmr->get_nama_dosen($nim);
        $data['hash'] = $this->uri->segment(4);
        $na['nama'] = $nm->nama;
        $na['nip'] = $nm->nip;
        $na['photo'] = $nm->pic;
        $this->parser->parse('dosen/header', $na);
        $this->load->view('dosen/nav-bar');
        $this->load->view('dosen/sidebar');
        $this->parser->parse('dosen/view-saran', $data);
        $this->load->view('dosen/footer');
    }

    //end tesis
    /* Start account */
    function password() {
        $nim = $this->session->userdata('username');
        $nm = $this->mmr->get_nama_dosen($nim);
        $na['nama'] = $nm->nama;
        $na['nip'] = $nm->nip;
        $na['photo'] = $nm->pic;
        $this->parser->parse('dosen/header', $na);
        $this->load->view('dosen/nav-bar');
        $this->load->view('dosen/sidebar');
        $this->load->view('dosen/password');
        $this->load->view('dosen/footer');
    }

    function passNew() {
        $id = $this->session->userdata('username');
        $update = array(
            'password' => $this->encrypt->sha1($this->input->post('pass'))
        );
        $this->mmr->updatePass($id, $update);
        redirect('dosen/passSuc');
    }

    function passSuc() {
        $nim = $this->session->userdata('username');
        $nm = $this->mmr->get_nama($nim);
        $na['nama'] = $nm->nama;
        $na['nim'] = $nm->nim;
        $data['menu'] = $this->mmr->menu_surat($nim);
        $data['stat'] = $this->mmr->stat($nim);
        if ($this->mmr->menu_profil($nim)) {
            $data['profil'] = $this->mmr->menu_profil($nim);
        } else {
            $data['profil'] = array();
        }
        $this->parser->parse('dosen/header', $na);
        $this->load->view('dosen/nav-bar');
        $this->load->view('dosen/sidebar', $data);
        $this->load->view('dosen/passSuc', $data);
        $this->load->view('dosen/footer');
    }

    function profil() {
        $nim = $this->session->userdata('username');
        $nm = $this->mmr->get_nama_dosen($nim);
        $na['nama'] = $nm->nama;
        $na['nip'] = $nm->nip;
        $na['photo'] = $nm->pic;
        $data['menu'] = $this->mmr->menu_surat($nim);
        $data['stat'] = $this->mmr->stat($nim);
//        $em = $this->mmr->email_dsn($nim);
//        $data['email'] = $em->email;
        $data = array(
            'nip' => $nm->nip,
            'nama' => $nm->nama,
            'email' => $nm->email,
            'telp' => $nm->telp,
            'pic' => $nm->pic
        );
        if ($this->mmr->menu_profil($nim)) {
            $data['profil'] = $this->mmr->menu_profil($nim);
        } else {
            $data['profil'] = array();
        }
        $this->parser->parse('dosen/header', $na);
        $this->load->view('dosen/nav-bar');
        $this->load->view('dosen/sidebar', $data);
        $this->parser->parse('dosen/profil', $data);
        $this->load->view('dosen/footer');
    }

    function emailNew() {
        $id = $this->session->userdata('username');
        $email = array(
            'email' => $this->input->post('email')
        );
        $this->mmr->updateMail($id, $email);
        redirect('dosen/email');
    }

    /* End account */

    function uploadFoto() {
        $nim = $this->session->userdata('username');
        $nm = $this->mmr->get_nama_dosen($nim);
        $data['photo'] = $nm->pic;
        $na['nama'] = $nm->nama;
        $na['nip'] = $nm->nip;
        $na['photo'] = $nm->pic;
        $this->parser->parse('dosen/header', $na);
        $this->load->view('dosen/nav-bar');
        $this->load->view('dosen/sidebar');
        $this->parser->parse('dosen/photo', $data);
        $this->load->view('dosen/footer');
    }

    function photo() {
        $config['upload_path'] = './photo/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1024';
        $config['max_width'] = '600';
        $config['max_height'] = '600';
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload()) {
            $nim = $this->session->userdata('username');
            $nm = $this->mmr->get_nama_dosen($nim);
            $data = array(
                'photo' => $nm->pic,
                'error' => $this->upload->display_errors()
            );
            $na['nama'] = $nm->nama;
            $na['nip'] = $nm->nip;
            $na['photo'] = $nm->pic;
            $this->parser->parse('dosen/header', $na);
            $this->load->view('dosen/nav-bar');
            $this->load->view('dosen/sidebar');
            $this->parser->parse('dosen/photoError', $data);
            $this->load->view('dosen/footer');
        } else {
            $file = $this->upload->data();
            $id = $this->session->userdata('username');
            $input = array(
                'pic' => base_url() . 'photo/' . $file['file_name'],
            );
            $this->mmr->upload_photo($id, $input);
            redirect('dosen/profil');
//            $data = array('upload_data' => $this->upload->data());
////            $this->load->view('upload_success', $data);
//            echo $data;
        }
    }

    function email() {
        $id = $this->session->userdata('username');
        $data = array(
            'nama' => $this->input->post('nama'),
            'email' => $this->input->post('email'),
            'telp' => $this->input->post('telp')
        );
        $this->mmr->updateProfilDosen($id, $data);
        redirect('dosen/profil');
    }

}

?>
